package com.equdao.car_violation_inquiry.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.equdao.car_violation_inquiry.pojo.BasicCarDO;
import com.equdao.car_violation_inquiry.pojo.Result;
import com.equdao.car_violation_inquiry.pojo.WebPageViolationDetailDO;
import com.equdao.car_violation_inquiry.service.BasicCarService;
import com.equdao.car_violation_inquiry.service.WebPageViolationDetailService;
import com.equdao.car_violation_inquiry.utils.ResultUtil;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @Author: 杨忠强
 * @Date: 2018/7/3 14:49
 * @Description:
 */

@RestController
public class ExportFileController {
    @Autowired
    private WebPageViolationDetailService webPageViolationDetailService;
    @Autowired
    private BasicCarService basicCarService;
    /**
     * 根据标识号查询明细数据
     * */
    @GetMapping("/exportFile")
    public void ExportFile(String flag,HttpServletResponse response) throws Exception {
        List<WebPageViolationDetailDO> violationDetailByFlag = webPageViolationDetailService.findViolationDetailByFlag(flag);

        System.out.println("查询到的数据条数=====>"+violationDetailByFlag.size());
        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams(), WebPageViolationDetailDO.class, violationDetailByFlag);

        String excelFileName = "violationDetail"+".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-disposition", "attachment;filename=" + excelFileName);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.flush();
                os.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * 根据批次号查询网络故障数据
     * */
    @GetMapping("/exportBasicData")
    public void exportBasicData(String flag, HttpServletResponse response) throws Exception {
        System.out.println("接收到的参数"+flag);

        List<BasicCarDO> list = basicCarService.findExportBasicData(flag);
        System.out.println("总共查询出"+list.size()+"条数据");
        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams(), BasicCarDO.class, list);
        String excelFileName =  "NetFault" + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-disposition", "attachment;filename=" + excelFileName);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.flush();
                os.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 根据批次号导出车辆信息异常原数据,并修改原数据状态为3(异常车辆)
     * */
    @GetMapping("/exportErrorBasicData")
    public void exportErrorBasicData(String flag, HttpServletResponse response) throws Exception {
        List<BasicCarDO> list = basicCarService.findExportViolationBasicData(flag);
        System.out.println("输出异常数据量"+list.size());
        //批量修改基本表异常信息
        Integer integer = basicCarService.updateBasicDataStatus(flag);
        System.out.println("修改数据量"+integer);

        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams(), BasicCarDO.class, list);
        String excelFileName = "ErrorData" + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-disposition", "attachment;filename=" + excelFileName);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.flush();
                os.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




}
